﻿using CSApp.BC;
using CSApp.Properties;
using CSLib.Imaging;
using FSLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace CSApp
{
	public partial class AppForm : Form
	{
		/// <summary>
		/// Constructor.
		/// </summary>
		public AppForm()
		{
			InitializeComponent();
		}

		#region EventHandlers.

		/// <summary>
		/// フォームロード時の処理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AppForm_Load(object sender, EventArgs e)
		{
			this.reloadSettings();
		}

		/// <summary>
		/// Reloadボタンをクリックしたときの処理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReload_Click(object sender, EventArgs e)
		{
			this.reloadSettings();
		}

		/// <summary>
		/// フィルタ適用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnCompute_Click(object sender, EventArgs e)
		{
			this.applyFilter();
		}

		#endregion

		// ================================================================

		/// <summary>
		/// 画像ファイル、プラグインのリロード
		/// </summary>
		private void reloadSettings()
		{
			// 項目クリア
			this.cmbSource.Items.Clear();
			this.cmbFilter.Items.Clear();

			// 画像ソースの読み込み
			DirectoryInfo di = new DirectoryInfo(Settings.Default.SourcePath);
			if (!di.Exists)
			{
				string msg = string.Format("Source directory not found. {0}", Settings.Default.SourcePath);
				MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			FileInfo[] fis = di.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
			this.cmbSource.Items.AddRange(fis);
			if (this.cmbSource.Items.Count > 0) this.cmbSource.SelectedIndex = 0;

			// プラグインの読み込み
			List<FilterItem> filterItems = FilterManager.EnumFilters();
			cmbFilter.Items.AddRange(filterItems.ToArray());
			if (this.cmbFilter.Items.Count > 0) this.cmbFilter.SelectedIndex = 0;

		}

		/// <summary>
		/// フィルタの適用
		/// </summary>
		private void applyFilter()
		{
			// 画像ファイルの読み込み
			string src = Path.Combine(Settings.Default.SourcePath, this.cmbSource.Text);
			BitmapEx bmp = new BitmapEx(src);

			// 対象フィルタの取得
			FilterItem filterItem = this.cmbFilter.SelectedItem as FilterItem;
			if (filterItem == null)
			{
				MessageBox.Show("Invalid filter.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			// 処理実行
			// NOTE:フィルタの種別が増えたら、ここに追加
			switch (filterItem.FilterType)
			{
				case FilterType.Filter2D:
					IFilter2D inst2D = (IFilter2D)filterItem.Instance;
					bmp.SetMat2D(inst2D.Compute(bmp.GetMat2D()));
					break;

				case FilterType.Filter3D:
					IFilter3D inst3D = (IFilter3D)filterItem.Instance;
					bmp.SetMat3D(inst3D.Compute(bmp.GetMat3D()));
					break;

				case FilterType.Analysis:
					IAnalysis ana = (IAnalysis)filterItem.Instance;
					string result = ana.Compute(bmp.GetValues());

					MessageForm msgForm = new MessageForm();
					msgForm.ShowMessage(result);
					break;
			}

			// 画像表示
			this.picImage.Image = bmp.Bmp;
		}


		private void cmbSource_TextChanged(object sender, EventArgs e)
		{
			// 画像ファイルの読み込み
			string src = Path.Combine(Settings.Default.SourcePath, this.cmbSource.Text);
			BitmapEx bmp = new BitmapEx(src);

			// 画像表示
			this.picImage.Image = bmp.Bmp;

		}

	}
}
